<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Biz\AdminUser;
use App\Request\AdminUserLoginRequest;
use App\Request\AdminUpdatePasswordRequest;
use App\Utils\Log;
use App\Utils\Json;
use Phper666\JWTAuth\JWT;

class AdminUserController extends AbstractController
{

    /**
     * 用户登录
     * @param AdminUserLoginRequest $request
     * @param JWT $jwt
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function login(AdminUserLoginRequest $request,JWT $jwt)
    {
        //验证输入数据
        $data = $request->validated();
        //加入日志
        Log::getInstance()->info('用户登录', $data);
        //获取逻辑层
        $biz = new AdminUser();
        $result = $biz->login($data,$jwt,$this->request->getServerParams());
        //返回结果
        return $this->response->json( Json::returnArray('SUCCESS','ok',$result) );
    }


    /**
     * 获取用户详情
     * @param JWT $jwt
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getDetail(JWT $jwt)
    {
        $user = $jwt->getParserData();
        //加入日志
        Log::getInstance()->info('用户Token数据', $user);
        //获取逻辑层
        $biz = new AdminUser();
        $result = $biz->detail($user);
        //返回结果
        return $this->response->json( Json::returnArray('SUCCESS','ok',$result) );
    }

    /**
     * 修改密码
     * @param JWT $jwt
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function updatePassword(AdminUpdatePasswordRequest $request,JWT $jwt){
        //验证输入数据
        $data = $request->validated();
        //解析用户数据
        $user = $jwt->getParserData();
        //加入日志
        Log::getInstance()->info('用户Token数据', $user);
        //获取逻辑层
        $biz = new AdminUser();
        $result = $biz->updatePassword($user,$data);
        //返回结果
        return $this->response->json( Json::returnArray('SUCCESS','ok',$result) );
    }
}
